<template>
    <div class="RefreshTime">
        下次刷新{{str_time}}
    </div>
</template>

<script>
import {formatSeconds} from "@/utils/public"
export default {
    props: {
        refresh_cb: {
            type: Function,
            default: null
        },
        time_speed: {
            type: Number,
            default: 300, //秒
        }
    },
    name: 'RefreshTime',
    data() {
        return {
            timer: null,
            str_time: '',
            speed: 0,
        }
    },
    created() {
        this.speed = this.time_speed;
        this.onCountdown();
    },
    methods: {
        onCountdown() {
            let str_time = formatSeconds(this.speed);
            this.str_time = str_time;
            if(this.timer) {
                clearTimeout(this.timer);
                this.timer = null;
            }
            this.timer =  setTimeout(() => {
                this.speed -= 1;
                if(this.speed < 0) {
                    if(this.refresh_cb) {
                        this.refresh_cb();
                    }
                    this.speed = this.time_speed;
                }
                this.onCountdown();
            }, 1000);
        }
    }
}
</script>

<style lang="scss" scoped>

</style>